package com.tanhua.manager.listener;

import com.tanhua.commons.template.BaiduCensorTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用于动态内容审核
 */
@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "consumer-tanhua-publish")
public class PublishCensorListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;
    @Autowired
    private BaiduCensorTemplate censorTemplate;
    @Override
    public void onMessage(String publishId) {
        Publish publish = publishApi.findById(new ObjectId(publishId));

        if (publish.getState() == 0) {
            boolean textOk = censorTemplate.textCensor(publish.getTextContent());
            boolean imageOk = true;
            if (textOk) {
                imageOk = censorTemplate.imageBatchCensor(publish.getMedias());
            }
            publish.setState(textOk && imageOk ? 1 : 2);
            publishApi.updateState(publish);
        }
    }
}
